<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('住院登记列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li style="width: 280px;">
                            <label>患者：</label>
                            <select name="patientId" id="patientId" class="form-control" required>
                                <option th:each="patient:${tPatients}" th:value="${patient.id}"
                                        th:text="${patient.name}"></option>
                            </select>
                        </li>
                        <li>
                            <label>入院日期：</label>
                            <input type="text" class="time-input" placeholder="请选择入院日期" name="admissionDate"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:registration:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:registration:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:registration:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-info" onclick="discharge()" shiro:hasPermission="system:registration:discharge">
                <i class="fa fa-sign-out"></i> 出院办理
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:registration:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<!-- 出院抽屉 -->
<div class="custom-drawer" id="dischargeDrawer" style="display:none;">
    <div class="drawer-mask" onclick="closeDrawer()"></div>
    <div class="drawer-wrapper">
        <div class="drawer-header">
            <h4><i class="fa fa-hospital-o"></i> 出院办理</h4>
            <i class="fa fa-times close-icon" onclick="closeDrawer()"></i>
        </div>
        <div class="drawer-body">
            <form id="dischargeForm">
                <div class="form-section">
                    <h5 class="section-title"><i class="fa fa-user"></i> 患者信息</h5>
                    <div class="row">
                        <div class="col-md-4 info-item">
                            <label>姓名：</label>
                            <span id="dischargePatientName" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item" style="width: 300px">
                            <label>身份证号：</label>
                            <span id="dischargeIdNumber" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>年龄：</label>
                            <span id="dischargeAge" class="info-text"></span>
                        </div>
                    </div>
                </div>

                <div class="form-section">
                    <h5 class="section-title"><i class="fa fa-bed"></i> 住院信息</h5>
                    <div class="row">
                        <div class="col-md-4 info-item">
                            <label>科室：</label>
                            <span id="dischargeDept" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>病房：</label>
                            <span id="dischargeWard" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>床位：</label>
                            <span id="dischargeBed" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>主治医生：</label>
                            <span id="dischargeDoctor" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>责任护士：</label>
                            <span id="dischargeNurse" class="info-text"></span>
                        </div>
                    </div>
                </div>

                <div class="form-section">
                    <h5 class="section-title"><i class="fa fa-calculator"></i> 费用结算</h5>
                    <div class="row">
                        <div class="col-md-4 info-item">
                            <label>入院日期：</label>
                            <span id="dischargeAdmissionDate" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>出院日期：</label>
                            <span id="dischargeDate" class="info-text"></span>
                        </div>
                        <div class="col-md-4 info-item">
                            <label>住院天数：</label>
                            <span id="dischargeDays" class="info-text"></span>
                        </div>
                        <div class="col-md-12 info-item">
                            <label>应结费用：</label>
                            <span id="dischargeFee" class="fee-amount"></span>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="drawer-footer">
            <button type="button" class="btn btn-primary" onclick="confirmDischarge()">
                <i class="fa fa-check"></i> 确认出院
            </button>
            <button type="button" class="btn btn-default" onclick="closeDrawer()">
                <i class="fa fa-close"></i> 取消
            </button>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:registration:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:registration:remove')}]];
    var prefix = ctx + "system/registration";
    var currentDischargeId = null;

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "住院登记",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '住院登记表主键',
                    visible: false
                },
                {
                    field: 'patientName',
                    title: '患者'
                },
                {
                    field: 'admissionDate',
                    title: '入院日期'
                },
                {
                    field: 'estimatedDischargeDate',
                    title: '预计出院日期'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value) {
                        return $.table.selectDictLabel([[${@dict.getType('in_status')}]], value);
                    }
                },
                {
                    field: 'doctorName',
                    title: '医生'
                },
                {
                    field: 'nurseName',
                    title: '护士'
                },
                {
                    field: 'bedName',
                    title: '床位'
                },
                {
                    field: 'wardName',
                    title: '病房'
                },
                {
                    field: 'totalCost',
                    title: '总费用'
                },
                {
                    field: 'paymentStatus',
                    title: '支付状态',
                    formatter: function(value) {
                        return $.table.selectDictLabel([[${@dict.getType('pay_status')}]], value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var actions = [];
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        if (row.status === 0) {
                            actions.push('<a class="btn btn-info btn-xs" onclick="showDischarge(\'' + row.id + '\')"><i class="fa fa-sign-out"></i>出院</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function showDischarge(id) {
        $.ajax({
            url: prefix + "/getDischargeInfo",
            type: 'POST',
            data: {id: id},
            success: function(res) {
                console.log(res)
                var data = res.data;
                // 患者信息
                $('#dischargePatientName').text(data.patientName);
                $('#dischargeAge').text(data.age);
                $('#dischargeIdNumber').text(data.idNumber);
                // 住院信息
                $('#dischargeDept').text(data.deptName);
                $('#dischargeWard').text(data.wardNumber);
                $('#dischargeBed').text(data.bedNumber);
                $('#dischargeDoctor').text(data.doctorName);
                $('#dischargeNurse').text(data.nurseName);

                // 费用计算
                var admission = new Date(data.admissionDate);
                var discharge = new Date();
                var days = Math.ceil((discharge - admission) / (86400000)) || 1;
                var fee = days * 100;

                $('#dischargeAdmissionDate').text($.common.dateFormat(admission, "yyyy-MM-dd"));
                $('#dischargeDate').text($.common.dateFormat(discharge, "yyyy-MM-dd"));
                $('#dischargeDays').text(days + " 天");
                $('#dischargeFee').text("¥" + fee.toFixed(2));

                currentDischargeId = id;
                $('#dischargeDrawer').show();
            }
        });
    }

    function confirmDischarge() {
        layer.confirm('<span class="text-danger">请确认已完成费用结算！</span>', {
            title: '出院确认',
            btn: ['确认','取消'],
            icon: 3
        }, function() {
            $.ajax({
                url: prefix + "/completeDischarge",
                type: "POST",
                data: { id: currentDischargeId },
                success: function(res) {
                    if (res.code === 0) {
                        $.modal.msgSuccess(res.msg);
                        return;
                    }else {
                        $.modal.msgError("出院办理异常");
                    }
                    closeDrawer();
                    $.table.refresh();
                }
            });
        });
    }

    function closeDrawer() {
        $('#dischargeDrawer').hide();
        currentDischargeId = null;
    }
</script>

<style>
    /* 抽屉样式 */
    .custom-drawer {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 9999;
        display: none;
    }
    .drawer-mask {
        background: rgba(0,0,0,0.5);
        width: 100%;
        height: 100%;
    }
    .drawer-wrapper {
        position: fixed;
        right: 0;
        top: 0;
        width: 680px;
        height: 100%;
        background: #fff;
        box-shadow: -2px 0 15px rgba(0,0,0,0.1);
        overflow-y: auto;
    }
    .drawer-header {
        padding: 15px 20px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .drawer-header h4 {
        margin: 0;
        color: #1ab394;
    }
    .close-icon {
        cursor: pointer;
        font-size: 18px;
        color: #999;
    }
    .close-icon:hover {
        color: #666;
    }
    .drawer-body {
        padding: 20px;
    }
    .form-section {
        margin-bottom: 25px;
        background: #f8f9fa;
        border-radius: 5px;
        padding: 15px;
    }
    .section-title {
        color: #1ab394;
        font-size: 16px;
        margin-bottom: 15px;
    }
    .info-item {
        margin-bottom: 12px;
    }
    .info-item label {
        display: inline-block;
        width: 80px;
        color: #666;
    }
    .info-text {
        color: #333;
        font-weight: 500;
    }
    .fee-amount {
        font-size: 24px;
        color: #f56c6c;
        font-weight: bold;
    }
    .drawer-footer {
        padding: 15px 20px;
        border-top: 1px solid #eee;
        text-align: right;
    }
</style>
</body>
</html>